﻿using System;
using System.Configuration;

namespace SPDeployer.Core.Config
{
    public class SiteConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty(Definitions.Sites.DESCRIPTION, IsKey = false, IsRequired = false)]
        public string Description
        {
            get { return this[Definitions.Sites.DESCRIPTION].ToString(); }
            set { this[Definitions.Sites.DESCRIPTION] = value; }
        }

        [ConfigurationProperty(Definitions.Sites.LCID, IsKey = false, IsRequired = false)]
        public int Lcid
        {
            get { return Int32.Parse(this[Definitions.Sites.LCID].ToString()); }
            set { this[Definitions.Sites.LCID] = value; }
        }

        [ConfigurationProperty(Definitions.Sites.NAME, IsKey = false, IsRequired = false)]
        public string Name
        {
            get { return this[Definitions.Sites.NAME].ToString(); }
            set { this[Definitions.Sites.NAME] = value; }
        }

        [ConfigurationProperty(Definitions.Sites.OWNER_EMAIL, IsRequired = true)]
        public string OwnerEmail
        {
            get { return this[Definitions.Sites.OWNER_EMAIL].ToString(); }
            set { this[Definitions.Sites.OWNER_EMAIL] = value; }
        }
        
        [ConfigurationProperty(Definitions.Sites.OWNER_LOGIN, IsRequired = true)]
        public string OwnerLogin
        {
            get { return this[Definitions.Sites.OWNER_LOGIN].ToString(); }
            set { this[Definitions.Sites.OWNER_LOGIN] = value; }
        }

        [ConfigurationProperty(Definitions.Sites.OWNER_NAME, IsRequired = false)]
        public string OwnerName
        {
            get { return this[Definitions.Sites.OWNER_NAME].ToString(); }
            set { this[Definitions.Sites.OWNER_NAME] = value; }
        }

        [ConfigurationProperty(Definitions.Sites.URL, IsRequired = true)]
        public string Url
        {
            get { return this[Definitions.Sites.URL].ToString(); }
            set { this[Definitions.Sites.URL] = value; }
        }

        [ConfigurationProperty(Definitions.Sites.WEB_TEMPLATE, IsRequired = false)]
        public string WebTemplate
        {
            get { return this[Definitions.Sites.WEB_TEMPLATE].ToString(); }
            set { this[Definitions.Sites.WEB_TEMPLATE] = value; }
        }
        
        [ConfigurationProperty(Definitions.FEATURES_COLLECTION, IsRequired = false)]
        public FeatureConfigurationElementCollection Features
        {
            get { return (FeatureConfigurationElementCollection)this[Definitions.FEATURES_COLLECTION]; }
            set { this[Definitions.FEATURES_COLLECTION] = value; }
        }

        [ConfigurationProperty(Definitions.WEBS_COLLECTION, IsRequired = false)]
        public WebConfigurationElementCollection Webs
        {
            get { return (WebConfigurationElementCollection)this[Definitions.WEBS_COLLECTION]; }
            set { this[Definitions.WEBS_COLLECTION] = value; }
        }
    }

    [ConfigurationCollection(typeof(SiteConfigurationElement), AddItemName = "Site")]
    public class SiteConfigurationElementCollection : ConfigurationElementCollection
    {
        public SiteConfigurationElement this[int index]
        {
            get { return BaseGet(index) as SiteConfigurationElement; }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new SiteConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((SiteConfigurationElement)element).Url;
        }
    }
}